package com.dc.aimc.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Administrator
 * @version 1.0
 **/
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

//    /**
//     * 认证成功处理器
//     */
//    @Autowired
//    private AimcAuthenticationSuccessHandler successHandler;
//
//    /**
//     * 认证失败处理器
//     */
//    @Autowired
//    private AimcAuthenticationFailureHandler failureHandler;

    // 密码编码器
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    // 认证管理器
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * SpringSecurity拦截配置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 开启表单登录
        http.formLogin();
//        http.formLogin().successHandler(successHandler).failureHandler(failureHandler);
        // 对所有请求进行认证
        http.authorizeRequests().antMatchers("/auth/login").permitAll().anyRequest().authenticated();
        // 关闭csrf 开启跨域
        http.csrf().disable().cors();
    }
}
